/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.esdc.librarysystem.manager;

import com.esdc.librarysystem.datastate.BookLoanState;
import com.esdc.librarysystem.exception.ExistException;
import com.esdc.librarysystem.entity.BookEntity;
import com.esdc.librarysystem.entity.TitleEntity;
import com.esdc.librarysystem.exception.NotExistException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author shou1dwe
 */
@Stateful
public class CollectionManagerBean implements CollectionManagerRemote {
    @PersistenceContext
    EntityManager entityManger;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void addTitle(String name,String ISBN, String author, String publisher) throws ExistException {
        TitleEntity title = entityManger.find(TitleEntity.class, ISBN);
        if(title != null){
           throw new ExistException("Title exists.");
        }

        title = new TitleEntity();
        title.create(ISBN, author, publisher, name);
        entityManger.persist(title);
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public long addBook(String ISBN) throws NotExistException {
        TitleEntity title = entityManger.find(TitleEntity.class, ISBN);
        if(title == null){
            throw new NotExistException("Title does not exist.");
        }
        BookEntity book = new BookEntity();
        Set books = title.getBooks();
        books.add(book);
        title.setBooks(books);
        
        book.setTitle(title);
        book.setCopyNumber(books.size());
        entityManger.persist(book);
        return book.getId();
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void deleteBook(long id) throws NotExistException {
        BookEntity book = entityManger.find(BookEntity.class, id);
        if(book == null) throw new NotExistException("Book not exist");
        entityManger.remove(book);
    }

    public List findBooksByTitle(String title) {
        Query query = entityManger.createQuery("SELECT b.id, b.copyNumber, t.title, t.author, t.publisher, b.dueDate from BookEntity b JOIN b.title t WHERE t.title LIKE :title");
        query.setParameter("title", title);
        List<BookLoanState> bookStates = findBook(query);
        return bookStates;
    }

    public List findBooksByAuthor(String author) {
        Query query = entityManger.createQuery("SELECT b.id, b.copyNumber, t.title, t.author, t.publisher, b.dueDate from BookEntity b JOIN b.title t WHERE t.author LIKE :author");
        query.setParameter("author", author);
        List<BookLoanState> bookStates = findBook(query);
        return bookStates;
    }

    private List<BookLoanState> findBook(Query query) {
        List books = query.getResultList();
        List<BookLoanState> bookStates = new ArrayList<BookLoanState>();
        for (Object object : books) {
            Object[] values = (Object[]) object;
            bookStates.add(new BookLoanState((Long) values[0], (Integer) values[1], (String) values[2], (String) values[3], (String) values[4], (Date) values[5]));
        }
        return bookStates;
    }
    
}
